import { defineStore } from 'pinia'
import { scheduleApi } from '@/api/schedule'

// 默认事件数据 - 添加3.13-3.21的日程数据
const defaultEvents = [
  {
    id: 1,
    title: '团队会议',
    date: new Date('2024-03-13'),
    time: '10:00',
    type: 'primary',
    description: '与团队讨论本周工作进展'
  },
  {
    id: 2,
    title: '项目演示',
    date: new Date('2024-03-14'),
    time: '14:30',
    type: 'danger',
    description: '向客户展示项目最新进展'
  },
  {
    id: 3,
    title: '健身',
    date: new Date('2024-03-15'),
    time: '18:00',
    type: 'success',
    description: '去健身房锻炼'
  },
  {
    id: 4,
    title: '项目评审',
    date: new Date('2024-03-16'),
    time: '09:30',
    type: 'primary',
    description: '与产品团队评审新功能'
  },
  {
    id: 5,
    title: '周会',
    date: new Date('2024-03-17'),
    time: '10:00',
    type: 'primary',
    description: '部门周会'
  },
  {
    id: 6,
    title: '技术分享',
    date: new Date('2024-03-18'),
    time: '15:00',
    type: 'info',
    description: 'Vue3与前端最新技术分享'
  },
  {
    id: 7,
    title: '客户拜访',
    date: new Date('2024-03-19'),
    time: '13:30',
    type: 'danger',
    description: '拜访重要客户讨论合作事宜'
  },
  {
    id: 8,
    title: '项目启动会',
    date: new Date('2024-03-20'),
    time: '09:00',
    type: 'primary',
    description: '新项目启动会议'
  },
  {
    id: 9,
    title: '团建活动',
    date: new Date('2024-03-21'),
    time: '16:00',
    type: 'success',
    description: '团队建设活动'
  }
]

export const useScheduleStore = defineStore('schedule', {
  state: () => ({
    events: [...defaultEvents],  // 初始化为默认数据
    loading: false,
    useLocalData: true  // 默认使用本地数据
  }),

  actions: {
    async fetchEvents(params) {
      this.loading = true
      try {
        const data = await scheduleApi.getEvents(params)
        this.events = data
        this.useLocalData = false
      } catch (error) {
        console.warn('使用本地日程数据', error)
        // 如果已有数据，不覆盖
        if (this.events.length === 0) {
          this.events = [...defaultEvents]
        }
        this.useLocalData = true
      } finally {
        this.loading = false
      }
    },

    async createEvent(eventData) {
      try {
        if (this.useLocalData) {
          const newEvent = {
            ...eventData,
            id: Date.now()
          }
          this.events.push(newEvent)
          return newEvent
        } else {
          const data = await scheduleApi.createEvent(eventData)
          this.events.push(data)
          return data
        }
      } catch (error) {
        console.warn('创建日程失败，使用本地模式', error)
        this.useLocalData = true
        const newEvent = {
          ...eventData,
          id: Date.now()
        }
        this.events.push(newEvent)
        return newEvent
      }
    },

    async updateEvent(id, eventData) {
      try {
        if (this.useLocalData) {
          const index = this.events.findIndex(e => e.id === id)
          if (index !== -1) {
            this.events[index] = { ...this.events[index], ...eventData }
            return this.events[index]
          }
          throw new Error('日程不存在')
        } else {
          const data = await scheduleApi.updateEvent(id, eventData)
          const index = this.events.findIndex(e => e.id === id)
          if (index !== -1) {
            this.events[index] = data
          }
          return data
        }
      } catch (error) {
        console.warn('更新日程失败，使用本地模式', error)
        this.useLocalData = true
        const index = this.events.findIndex(e => e.id === id)
        if (index !== -1) {
          this.events[index] = { ...this.events[index], ...eventData }
          return this.events[index]
        }
        throw error
      }
    },

    async deleteEvent(id) {
      try {
        if (!this.useLocalData) {
          await scheduleApi.deleteEvent(id)
        }
        this.events = this.events.filter(e => e.id !== id)
      } catch (error) {
        console.warn('删除日程失败，使用本地模式', error)
        this.useLocalData = true
        this.events = this.events.filter(e => e.id !== id)
      }
    }
  }
}) 